package com.xyz.keshe;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xyz.keshe.adapter.CategoryManageAdapter;
import com.xyz.keshe.dao.CategoryDao;
import com.xyz.keshe.db.DatabaseHelper;
import com.xyz.keshe.model.Category;

import java.util.List;

public class CategoryManageActivity extends AppCompatActivity {

    private ListView listViewCategories;
    private Button btnAddCategory;
    private CategoryManageAdapter adapter;
    private CategoryDao categoryDao;
    private List<Category> categoryList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_manage);

        // 初始化数据库
        categoryDao = new CategoryDao(this);

        // 初始化视图
        listViewCategories = findViewById(R.id.listview_categories);
        btnAddCategory = findViewById(R.id.btn_add_category);

        // 加载栏目列表
        loadCategories();

        // 添加栏目按钮点击事件
        btnAddCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddCategoryDialog();
            }
        });
    }

    private void loadCategories() {
        categoryList = categoryDao.getAllCategories();
        adapter = new CategoryManageAdapter(this, categoryList, new CategoryManageAdapter.OnCategoryActionListener() {
            @Override
            public void onEdit(Category category) {
                showEditCategoryDialog(category);
            }

            @Override
            public void onDelete(Category category) {
                showDeleteConfirmDialog(category);
            }
        });
        listViewCategories.setAdapter(adapter);
    }

    private void showAddCategoryDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("添加栏目");

        View dialogView = getLayoutInflater().inflate(R.layout.dialog_add_category, null);
        EditText etName = dialogView.findViewById(R.id.et_category_name);
        EditText etDescription = dialogView.findViewById(R.id.et_category_description);

        builder.setView(dialogView);
        builder.setPositiveButton("添加", (dialog, which) -> {
            String name = etName.getText().toString().trim();
            String description = etDescription.getText().toString().trim();

            if (TextUtils.isEmpty(name)) {
                Toast.makeText(this, "栏目名称不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            Category category = new Category();
            category.setName(name);
            category.setDescription(description);

            long result = categoryDao.insertCategory(category);
            if (result > 0) {
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                loadCategories();
            } else {
                Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showEditCategoryDialog(Category category) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("编辑栏目");

        View dialogView = getLayoutInflater().inflate(R.layout.dialog_add_category, null);
        EditText etName = dialogView.findViewById(R.id.et_category_name);
        EditText etDescription = dialogView.findViewById(R.id.et_category_description);

        // 填充现有数据
        etName.setText(category.getName());
        etDescription.setText(category.getDescription());

        builder.setView(dialogView);
        builder.setPositiveButton("保存", (dialog, which) -> {
            String name = etName.getText().toString().trim();
            String description = etDescription.getText().toString().trim();

            if (TextUtils.isEmpty(name)) {
                Toast.makeText(this, "栏目名称不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            category.setName(name);
            category.setDescription(description);

            int result = categoryDao.updateCategory(category);
            if (result > 0) {
                Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
                loadCategories();
            } else {
                Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showDeleteConfirmDialog(Category category) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认删除");
        builder.setMessage("确定要删除栏目 \"" + category.getName() + "\" 吗？删除后该栏目下的所有新闻也将被删除。");
        builder.setPositiveButton("删除", (dialog, which) -> {
            int result = categoryDao.deleteCategory(category.getId());
            if (result > 0) {
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                loadCategories();
            } else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadCategories();
    }
} 